let req=require('./utils/request.js').Request;
let util=require('./utils/util.js');

var app=getApp();

var api={
  register:'/register',
  login:'/login'
}

const API={
  /**
 * 注册
 * @param {微信来的code} code 
 * @param {*} stuId 
 */
  register(code,stuId){
    console.log(app);
    let params='?';
  if(code!=null){
    params+='code='+code;
  }
  if(stuId!=null){
    params+='&stuId='+stuId;
  }
  wx.getUserProfile({
    desc: '为了更好的服务,需要绑定微信',
    success:function(res){
      req.post(api.register+params,res).then(function(res){
        util.msg(res.msg);
        console.log(res);
        let code=res.code;
        let data=res.dataMap;
        if(code==0){
          app.globalData.userInfo=data.user;
          app.globalData.uid=data.user.id;
          app.globalData.token=data.token;
          app.globalData.hasLogin=true;
          wx.setStorageSync('uid', data.user.id);
          wx.setStorageSync('token', data.token);
          wx.setStorageSync('hasLogin', true);
        }
      });
    }
  });
  },
  /**
   * 登陆
   * @param {*} uid 
   * @param {*} token 
   */
  login(uid,token){
    let params='?';
    if(uid!=null){
      params+='uid='+uid;
    }
    if(token!=null){
      params+='&token='+token;
    }
    req.get(api.login+params);
  }
}

module.exports={
  API
}